<template>
  <div class="userInfo">
    <!-- 头部 -->
    <div class="top">
      <div class="top1">
        <!-- 头像 -->
        <div class="avart" v-if="userInfo.photo" @click="userEdit">
          <img :src="userInfo.photo" alt="" />
        </div>
        <div class="avart" v-else>
          <img src="@/assets/logo.png" alt="" />
        </div>
        <!-- 申请认证 -->
        <div class="info">
          <div class="nickName">{{ userInfo.name }}</div>
          <div class="attestation"><p>申请认证</p></div>
        </div>
        <!-- 今日阅读 -->
        <div class="read">
          <div class="read1">
            <div class="icon">
              <i class="iconfont" style="font-size:17px">&#xe6c7;</i>
            </div>
            <div class="text">
              <div class="today">今日阅读</div>
              <div class="time">5分钟</div>
            </div>
          </div>
        </div>
      </div>
      <div class="top2">
        <!-- 动态 -->
        <div class="top2-item">
          <div class="num">8</div>
          <div class="text">动态</div>
        </div>
        <!-- 关注 -->
        <div class="top2-item">
          <div class="num">{{ total_attention }}</div>
          <div class="text">关注</div>
        </div>
        <!-- 粉丝 -->
        <div class="top2-item">
          <div class="num">{{ total_fans }}</div>
          <div class="text">粉丝</div>
        </div>
      </div>
    </div>
    <!-- 中间 -->
    <div class="middle">
      <!-- 收藏 -->
      <div class="middle-item">
        <div>
          <i class="iconfont">&#xe681;</i>
        </div>
        <div class="text">收藏</div>
      </div>
      <!-- 历史 -->
      <div class="middle-item">
        <div>
          <i class="iconfont">&#xe718;</i>
        </div>
        <div class="text">历史</div>
      </div>
      <!-- 作品 -->
      <div class="middle-item">
        <div>
          <i class="iconfont">&#xe6c4;</i>
        </div>
        <div class="text">作品</div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="bottom">
      <div class="bottom-item">
        <van-cell title="消息通知" is-link />
        <van-cell title="实名认证" is-link />
      </div>
      <div class="bottom-item">
        <van-cell title="用户反馈" is-link />
        <van-cell title="小智同学" is-link />
        <van-cell title="系统设置" is-link />
      </div>
      <div class="bottom-item">
        <van-cell />
      </div>
    </div>
  </div>
</template>

<script>
import { getLocal } from '@/utils/token.js'
import { userInfo, userFollowings, userFollowers } from '@/api/userInfo.js'
// import { mapState } from '@/store/index'
export default {
  data () {
    return {
      userInfo: [], // 储存用户个人信息
      total_attention: 0, // 关注的用户总数
      total_fans: 0 // 粉丝的用户总数
    }
  },
  methods: {
    userEdit () {
      this.$router.push('/userInfo/infoEdit')
    }
  },
  computed: {},
  async created () {
    // 判断用户是否登录
    if (this.$store.isLogin) {
      // 发送请求渲染数据
      const res = await userInfo()
      this.userInfo = res.data
    } else {
      // 但是有token，发送请求渲染数据
      if (getLocal()) {
        const res = await userInfo()
        // console.log(res)
        // 存储数据
        this.userInfo = res.data
        // 保存用户是否登录
        this.$store.commit('setIsLogin', true)
        this.$store.commit('setUserInfo', res.data)
        // 获取关注用户
        const res2 = await userFollowings({
          target: 1151041081534578700
        })
        this.total_count = res2.data.total_attention

        // 粉丝列表
        const res3 = await userFollowers({
          target: 1151041081534578700
        })
        this.total_count = res3.data.total_fans
      } else {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.userInfo {
  background-color: #f5f7f9;
  .top {
    background-color: #30404b;
    opacity: 0.7;
    padding: 31px 20px 16px;
    position: relative;
    .top1 {
      display: flex;
      .avart {
        width: 65px;
        height: 65px;
        justify-content: left;
        img {
          background-color: #fff;
          border-radius: 50%;
          width: 100%;
        }
      }
      .info {
        padding-left: 15px;
        padding-top: 13px;
        .nickName {
          font-family: MicrosoftYaHei;
          font-size: 15px;
          font-weight: normal;
          font-stretch: normal;
          letter-spacing: 0px;
          color: #ffffff;
        }
        .attestation {
          width: 58px;
          height: 16px;
          background-color: #ffffff;
          border-radius: 8px;
          text-align: center;
          p {
            font-family: MicrosoftYaHei-Bold;
            font-size: 10px;
            font-weight: normal;
            font-stretch: normal;
            letter-spacing: 0px;
            color: #3296fa;
            line-height: 16px;
          }
        }
      }
      .read {
        position: absolute;
        right: 0px;
        width: 94px;
        height: 40px;
        background-color: #fff;
        border-radius: 20px 0px 0px 20px;
        opacity: 0.7;
        margin-top: 13px;
        .read1 {
          display: flex;
          justify-content: space-between;
          .icon {
            width: 16px;
            height: 17px;
            color: #000;
            padding: 3px 0 0 12px;
          }
          .text {
            padding-left: 6px;
            .today {
              font-family: MicrosoftYaHei;
              font-size: 6px;
              font-weight: normal;
              font-stretch: normal;
              letter-spacing: 0px;
              color: #000;
              padding-top: 8px;
            }
            .time {
              font-family: MicrosoftYaHei;
              font-size: 6px;
              font-weight: normal;
              font-stretch: normal;
              letter-spacing: 0px;
              color: #000;
            }
          }
        }
      }
    }
    .top2 {
      display: flex;
      justify-content: space-between;
      text-align: center;
      color: #fff;
      padding: 0 20px;
      .top2-item {
        padding-top: 27.5px;
        .num {
          font-family: ArialMT;
          font-size: 18px;
          font-weight: normal;
          font-stretch: normal;
          letter-spacing: 1px;
          color: #ffffff;
        }
        .text {
          font-family: MicrosoftYaHei;
          font-size: 12px;
          font-weight: normal;
          font-stretch: normal;
          letter-spacing: 1px;
          color: #ffffff;
        }
      }
    }
  }
  .middle {
    padding: 13px 40px 15px;
    display: flex;
    justify-content: space-between;
    text-align: center;
    background-color: #fff;
    .iconfont {
      color: #ec3a4e;
      font-size: 23px;
    }
    .text {
      padding-top: 6px;
      font-family: MicrosoftYaHei;
      font-size: 14px;
      font-weight: normal;
      font-stretch: normal;
      letter-spacing: 1px;
      color: #333333;
    }
  }
  .bottom {
    .bottom-item {
      margin-top: 5px;
      margin: 5px 0;
    }
  }
}
</style>
